package com.service.sys;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.base.BaseService;
import com.entity.sys.SysDict;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.sys.SysDictMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 字典 服务实现类
 */
@Service
public class SysDictService extends BaseService<SysDictMapper, SysDict>{

    /**
     * 字典类型列表
     */
    public List<SysDict> dictTypeList(Map<String,String> map){
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_type",1);
        if (StrUtil.isNotBlank(map.get("name"))){
            queryWrapper.like("name",map.get("name"));
        }
        queryWrapper.orderByDesc("parent_id,sort");
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 字典项列表
     */
    public PageInfo<SysDict> page(Map<String,String> map) {
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(map.get("name"))) {
            queryWrapper.like("name", map.get("name"));
        }
        if(StrUtil.isNotBlank(map.get("name"))) {
            queryWrapper.like("code", map.get("code"));
        }
        if (StrUtil.isNotBlank(map.get("name"))){
            queryWrapper.eq("parent_id",map.get("parentId"));
        }
        queryWrapper.ne("is_type", 1);
        queryWrapper.orderByAsc("parent_id,sort");
        PageHelper.startPage(Integer.parseInt(map.get("pageNum")),Integer.parseInt(map.get("pageSize")));
        List<SysDict> list = baseMapper.selectList(queryWrapper);
        return new PageInfo<>(list);
    }

    private List<SysDict> list(String dictName, String dictCode){
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(dictName)){
            queryWrapper.likeRight("name",dictName);
        }
        if (StrUtil.isNotBlank(dictCode)){
            queryWrapper.likeRight("code",dictCode);
        }
        queryWrapper.select("name,code");
        queryWrapper.orderByDesc("create_time");
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 生成字段名称或字典代码
     */
    public String createNameOrCode(String nameOrCode,String create){
        int strIndex = nameOrCode.lastIndexOf("_");
        if (strIndex != -1){
            nameOrCode = nameOrCode.substring(strIndex+1);
            boolean isInt = NumberUtil.isInteger(nameOrCode);
            if (isInt){
                int i = Integer.parseInt(nameOrCode)+1;
                create = create + "_" + i;
            }else {
                create =  create+ "_" + 1;
            }
        }else {
            create =  create+ "_" + 1;
        }
        return create;
    }

    /**
     * 生成字典名称
     */
    public String createName(String dictName){
        int index = dictName.lastIndexOf("_");
        if (index != -1){
            dictName = dictName.substring(0,index);
        }
        List<SysDict> list = list(dictName, null);
        if (!list.isEmpty()) {
            return createNameOrCode(list.get(0).getName(),dictName);
        }
        return dictName;
    }

    /**
     * 生成字典代码
     */
    public String createCode(String dictCode){
        int index = dictCode.lastIndexOf("_");
        if (index != -1){
            dictCode = dictCode.substring(0,index);
        }
        List<SysDict> list = list(null, dictCode);
        if (!list.isEmpty()) {
            return createNameOrCode(list.get(0).getCode(),dictCode);
        }
        return dictCode;
    }

    /**
     * 判断字典名称和编码是否已存在
     */
    public boolean hasDictByNameAndCode(String dictName,String dictCode,String id) {
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(dictCode)){
            queryWrapper.eq("code",dictCode);
        }
        if (StrUtil.isNotBlank(dictName)){
            queryWrapper.eq("name",dictName);
        }
        if (StrUtil.isNotBlank(id)){
            queryWrapper.ne("id",id);
        }
        Integer count = baseMapper.selectCount(queryWrapper);
        return count > 0 ? true : false;
    }

    /**
     * 判断字典类型是否存在子级
     */
    public boolean hasDictByParent(String id) {
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count > 0 ? true : false;
    }

    /**
     * 根据字典编码查询字典
     */
    public SysDict getByDictCode(String dictCode){
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code",dictCode);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据字典类型编码查询字典项列表
     */
    public List<SysDict> getListByDictCode(String dictCode){
        SysDict dict = getByDictCode(dictCode);
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",dict.getId());
        queryWrapper.select("id,name");
        return baseMapper.selectList(queryWrapper);
    }
}
